package com.ekingstar.framework.action;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;

import com.opensymphony.xwork2.ActionChainResult;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.service.IUserService;
import com.ekingstar.framework.service.UserExistException;

@Action(name = "cpwd", namespace = "/user")
@Results( {
		@Result(name = "success", value = "/user/success.jsp"),
		@Result(name = "error", value = "/user/error.jsp"),
		@Result(name = "nologin", value = "/user/nologin.jsp"),
		@Result(name = "input", value = "cpwd", type = ActionChainResult.class, params = {
				"namespace", "/user", "method", "input" }) })
public class ChangePassworkAction extends ActionSupport implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IUserService userService;
	private User user;

	private String npwd;
	private String nfpwd;
	private Map universitys;

	private Map userTypes;

	public Map getUserTypes() {
		return userTypes;
	}

	public Map getUniversitys() {
		return universitys;
	}

	public void setUniversitys(Map universitys) {
		this.universitys = universitys;
	}

	public void setUserTypes(Map userTypes) {
		this.userTypes = userTypes;
	}

	public String input() {
		this.user = UserUtil.getCurrentUser();
		if (this.user.getName().equals("") || this.user.getName() == null
				|| this.user.getName().trim().equals("roleAnonymous")) {
			return "nologin";
		}
		return "init";
	}

	/*@Validations(requiredStrings = {
			@RequiredStringValidator(message = "原密码必填", fieldName = "user.pwd", shortCircuit = true),
			@RequiredStringValidator(message = "新密码必填", fieldName = "npwd", shortCircuit = true),
			@RequiredStringValidator(message = "确认密码必填", fieldName = "nfpwd", shortCircuit = true) }, expressions = { @ExpressionValidator(message = "两次输入密码不一致", expression = "npwd.equals(nfpwd)", shortCircuit = true)

	}, regexFields = { @RegexFieldValidator(message = "密码不安全(6-20位)", fieldName = "npwd", expression = "[0-9a-zA-Z]{6,20}", shortCircuit = true) }

	)*/
	public String sub() {
		try {
			if (!this.userService.checkPassword(this.user)) {
				throw new UserExistException("原密码不正确!");
			}
			if(!this.npwd.trim().equals(this.nfpwd.trim())){ 
				throw new UserExistException("两次密码输入不匹配!"); 
			}
			this.userService.changePWD(this.user, this.npwd);
		} catch (UserExistException e) {
			this.addFieldError(ERROR,e.getMessage());
			this.addActionError(e.getMessage());
			this.user = UserUtil.getCurrentUser();
			return "input";
		}

		return "success";
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getNpwd() {
		return npwd;
	}

	public void setNpwd(String npwd) {
		this.npwd = npwd;
	}

	public String getNfpwd() {
		return nfpwd;
	}

	public void setNfpwd(String nfpwd) {
		this.nfpwd = nfpwd;
	}

	public void prepare() throws Exception {

		// TODO Auto-generated method stub
		
		if (userTypes == null) {
			userTypes = new LinkedHashMap();
			User.Type[] uts = User.Type.values();
			for (int i = 0 ;i  < uts.length ; i++){
				userTypes.put(uts[i].name(), uts[i].toString());
			}
		}
	

	}
}
